#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import messagebox
from tkinter import scrolledtext
import threading
from tkinter.filedialog import askopenfilename
from client import *
import time
import socket

server_IP = '127.0.0.1'
server_port = 12345
class LoginWindow():

    def __init__(self):

        window = tk.Tk()
        window.title('登录&注册')
        self.c=0
        self.x=0
        pic = tk.Canvas(window,height=300,width=520)
        image_file = tk.PhotoImage(file='pic.gif')
        image = pic.create_image(0, 0, anchor='nw', image=image_file)
        usernametext = tk.Label(window, text="Username")
        usernameguess = tk.Entry(window)
        passwdtext = tk.Label(window, text="Password")
        passwdguess = tk.Entry(window,show='*')
        loginButton = tk.Button(text="Login")

        loginButton.bind("<Button-1>", lambda event: self.loginClick( usernameguess, passwdguess, window))

        usernametext.grid(row=2,column=3,columnspan=2)
        usernameguess.grid(row=3,column=3,columnspan=2)
        pic.grid(row=1,column=1,columnspan=6)
        passwdtext.grid(row=4,column=3,columnspan=2)
        passwdguess.grid(row=5,column=3,columnspan=2)
        loginButton.grid(row=6, column=3, columnspan=2)

        window.mainloop()

    def loginClick(self,  usernameguess, passwdguess, window):
        self.c = ClientPrj5((server_IP, server_port))
        if self.c.login(usernameguess.get(), passwdguess.get()):

            window.destroy()
            self.x=mainwindow(self.c)
        else:
            messagebox.showinfo(title='Error', message='登录失败！')
class mainwindow:
    def __init__(self,client):
        # 实例化object，建立窗口window
        self.c=client
        self.pub=0#标志请求公共文件夹
        self.pri = 0  # 标志请求私人文件夹
        self.c.run()
        self.c.autoDown()
        self.path=0 #记录选择的文件路径
        self.window=tk.Tk()
        self.window.title('G7聊天器')# 给窗口的可视化起名字
        self.window.geometry('383x520')  # 设定窗口的大小(长 * 宽)
        self.l2 = tk.Label(self.window, text="私密文件夹",font=('Arial', 12),height=1)
        self.l2.grid(row=5, column=1)
        self.l3 = tk.Label(self.window, text="公共文件夹",font=('Arial', 12),height=1)
        self.l3.grid(row=3, column=1)
        self.l4 = tk.Label(self.window, text="用户列表",font=('Arial', 12),height=1)
        self.l4.grid(row=0, column=1)
        self.en = tk.Text(self.window,  bg='Thistle', fg='black', font=('Arial', 12), width=10, height=1)
        self.en.grid(row=2, column=1)
        #设置聊天输入的文本框
        self.scr = scrolledtext.ScrolledText(self.window,bg='LightSteelBlue', font=('Arial', 12),width=40, height=10, wrap=tk.WORD)
        self.scr.grid(row=6,column=0)
        self.l1 = scrolledtext.ScrolledText(self.window,  bg='PowderBlue', fg='black', font=('Arial', 12), width=40, height=22)
        self.l1.grid(row=0, column=0, rowspan=5)
        self.frmLT = tk.Frame(width=200, height=10, bg='white')
        self.frmLT.grid(row=5, column=0,sticky='w')
        #定义一个listbox，作为用户列表
        self.la = tk.Listbox(self.window,bg='Thistle',font=('Arial', 12),width=10, height=9,borderwidth=0)
        self.la.grid(row=1, column=1)
        self.lb = tk.Listbox(self.window, bg='PaleTurquoise', font=('Arial', 12),width=10, height=10, borderwidth=0)
        self.lb.grid(row=4, column=1)
        self.lc = tk.Listbox(self.window, bg='SkyBlue',font=('Arial', 12), width=10, height=10, borderwidth=0)
        self.lc.grid(row=6, column=1)
        self.b1 = tk.Button(self.frmLT, text='文件', command=self.file_selection, font=('Arial', 12),
                            height=1)
        self.b1.pack(side='left')

        self.b3 = tk.Button(self.frmLT, text='上传', activebackground='LightPink', command=self.up, font=('Arial', 12),
                            height=1)
        self.b3.pack(side='left')
        self.b4 = tk.Button(self.frmLT, text='下载', activebackground='LightPink', command=self.down, font=('Arial', 12),
                            height=1)
        self.b4.pack(side='left')
        self.b5 = tk.Button(self.frmLT, text='发送', activebackground='LightPink', command=self.send, font=('Arial', 12),
                            height=1)
        self.b5.pack(side='left')
        self.b8 = tk.Button(self.frmLT, text='离线发送', activebackground='LightPink', command=self.lisend, font=('Arial', 12),
                            height=1)
        self.b8.pack(side='left')
        self.b6 = tk.Button(self.frmLT, text='公共', activebackground='LightPink', command=self.pubRequest,
                            font=('Arial', 12),
                            height=1)
        self.b6.pack(side='left')
        self.b7 = tk.Button(self.frmLT, text='私密', activebackground='LightPink', command=self.priRequest,
                            font=('Arial', 12),
                            height=1)
        self.b7.pack(side='left')
        threading.Thread(target=self.check).start()
        self.window.mainloop()



    def pubRequest(self):
        self.pub=1
        self.pri=0
        self.c.publicFolder()
    def priRequest(self):
        self.pri=1
        self.pub=0
        self.c.privateFolder()


    def up(self):
        self.scr.delete('1.0','end')
        print(self.path[0])
        self.c.upRequest(self.path[0])

    def down(self):
        if self.pub:
            a=self.lb.get(self.lb.curselection())
            no=a.split('|')[0]
            self.c.downRequest(int(no))
        if self.pri:
            a=self.lb.get(self.lc.curselection())
            no=a.split('|')[0]
            self.c.downRequest(int(no))
    def send(self):
            txt = self.scr.get('0.0', 'end')
            name=self.la.get(self.la.curselection())
            num=name.split('|')[0]
            if txt[0]=='/':
                self.c.sendFileRequest(int(num), self.path[0])
                self.scr.delete('1.0', 'end')
            else:
                self.c.sendText(int(num), txt)
                self.scr.delete('1.0', 'end')
    def lisend(self):
        name= self.en.get('0.0', 'end')
        self.c.offLineTransRequest(name, self.path[0])
        self.scr.delete('1.0','end')

    def file_selection(self):
        filename = tk.filedialog.askopenfilenames()
        self.path=filename
        self.scr.insert('end', self.path[0])
        #print(self.path)


        # 定义button


    def check(self):
        while 1:
            m = self.c.msgQueue.get()
            if m[0] == 1:
                self.l1.insert('end', m[1])
                self.l1.insert(tk.INSERT, '\n')
                self.l1.see(tk.END)
            elif m[0] == 2:
                self.lc.delete(0, 'end')
                for i in m[1].keys():
                    self.lc.insert('end', (str(i) + '|' + m[1][i]))

            elif m[0] == 3:
                self.lb.delete(0, 'end')

                for i in m[1].keys():
                    self.lb.insert('end', (str(i) + '|' + m[1][i]))
            elif m[0] == 4:
                self.la.delete(0, 'end')
                for i in m[1].keys():
                    #self.la.insert('end', m[1][i])
                    self.la.insert('end', (str(i) + '|' + m[1][i]))





if  __name__ == "__main__":
    l=LoginWindow()




